﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Xcap
{
    public class DataRecievedEventArgs : EventArgs
    {
        private int messageStatus;
        private string messageHeaders;
        private string messageXml;

        public DataRecievedEventArgs(string data)
        {
            string[] lines = Regex.Split(data, "\r\n");
            string headers = "";                            //drzi headery vratene
            string body = "";                               //drzi xml odpoved zo servera
            bool separate = false;                          //nastavi na true ak uz oddelilo vsetky headery
            foreach (string line in lines)
            {
                if (separate == false)
                {
                    if (line != "")
                        headers += line + "\r\n";
                    else
                        separate = true;
                }
                else
                    body += line + "\r\n";
            }
            try
            {
                this.messageStatus = Convert.ToInt16(headers.Substring(9, 3));
            }
            catch(ArgumentOutOfRangeException)
            {
                //docasne poriesim takouto chybou, neviem kedy sa tento
                //exception moze vyskytnut..ak nedam header content 
                //length pri put ale neviem preco
                this.messageStatus = 555;
            }
            this.messageHeaders = headers;
            this.messageXml = body;
        }

        public int MessageStatus
        {
            get { return messageStatus; }
        }

        public string MessageHeaders
        {
            get { return messageHeaders; }
        }

        public string MessageXml
        {
            get { return messageXml; }
        }
    }

    /// <summary>
    /// Server response message statusses
    /// </summary>
    public enum xcapMessageStatus : short
    { 
        ok                  = 200,
        notFound            = 404,
        internalServerError = 500,
        forbidden           = 403,
        conflict            = 409
    }
    
    /// <summary>
    /// Various types of Xcap XML documents
    /// </summary>
    public enum xcapDocument : byte
    { 
        resourceLists,
        presRules,
        servCaps,
        pidf,
        /// <summary>
        /// Used for manupulation with entry in resource lists
        /// </summary>
        resourceListsEntry,
        /// <summary>
        /// Used for manipulation with pres rules One element
        /// </summary>
        presRulesIdentity
    }

    /// <summary>
    /// Allowed HTTP action methods (GET, PUT, DELETE)
    /// </summary>
    public enum xcapAction : byte
    {
        GET,
        PUT,
        DELETE
    }

    /// <summary>
    /// Supported pidf basic statuses
    /// </summary>
    public enum xcapBasicStatus : byte
    { 
        open,
        closed
    }

    /// <summary>
    /// Class returns specified Content-Type header for particular enum XcapDocument
    /// </summary>
    public static class XcapHeaders
    {
        //bacha, tuto musia sediet indexy podla xcapDocument enum
        public static string[] header = new string[]{
               "application/resource-lists+xml",    
               "application/auth-policy+xml",
               "application/xcap-caps+xml",
               "application/pidf+xml",
               //tuto dame pre rozne elementy viacero riadkov rovnakych aby to sedelo s xcapDocument
               "application/xcap-el+xml",
               "application/xcap-el+xml",
               //"application/rls-services+xml",
            };

        //headery
        //Content-Type:application/xcap-el+xml          - uprava iba elementu
        //Content-Type:application/xcap-caps+xml        - serv caps
        //Content-Type:application/resource-lists+xml  - resource list
        //Content-Type:application/rls-services+xml    - rls services
        //Content-Type:application/auth-policy+xml     - pri autentifikacii
        //Content-Type:application/pidf+xml             - pri pidf-manipulation
        public static string getHeader(xcapDocument index)
        {
            return header[(int)index];
        }
    }
}
